package com.example.win.android_banduyujia.ui.popupWindow;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.login.application.ILoginStatusChangedListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Win on 2015/9/14.
 */
public class Popupwindows extends PopupWindow implements View.OnClickListener {

    private View view;

    private LinearLayout  layoutPopup;
    private PopupWindow   popupMessage;

    private Button  btn1, btn2, btn3;

    public Popupwindows(Activity context) {
        LayoutInflater  inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.title_pic_item, null);

        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(view);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
         ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.anim.fade_ins);

        btn1 = (Button)view.findViewById(R.id.item_popupwindows_camera);
        btn2 = (Button)view.findViewById(R.id.item_popupwindows_Photo);
        btn3 = (Button)view.findViewById(R.id.item_popupwindows_cancel);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.item_popupwindows_camera:
                photo();
                break;
            case R.id.item_popupwindows_Photo:
                gotoPicActivity();
                break;
            case R.id.item_popupwindows_cancel:
                dismiss();
                break;
        }
    }

    private static final int TAKE_PICTURE = 0x000000;
    private String path = "";

    private void photo() {
        String  status = Environment.getExternalStorageState();
        if(status.equals(Environment.MEDIA_MOUNTED)){
            File file=new File(Environment.getExternalStorageDirectory() + "/image/");
            if(!file.exists())
                file.mkdirs();

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // 下面这句指定调用相机拍照后的照片存储的路径
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "jpg")));
            intent.putExtra("return-data", true);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,0);
            startActivityForResult(intent, TAKE_PICTURE);
        }
    }

    private void startActivityForResult(Intent intent, int takePicture) {

    }

    private void gotoPicActivity() {

    }


    public void showAtLocation(View parent) {
         if(!this.isShowing()){
             this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
         }else{
             this.dismiss();
         }
    }
}
